/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.batavia.impl;

import com.batavia.domain.AmbilFlight;
import com.batavia.domain.BantuLihatFlight;
import com.batavia.inter.AmbilFlightInterface;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.util.Cookie;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;

/**
 *
 * @author Saiful Bahri
 * blog    : http://bahrie27.wordpress.com/
 * twitter : @bahrie127
 */
public class ImplementAmbilFlight implements AmbilFlightInterface {

    BantuLihatFlight blf;

    public List<AmbilFlight> search(Cookie cookie, int radioKe, BantuLihatFlight blf) {

        try {

            this.blf = blf;
            this.blf.setRadioKe(radioKe);
            WebClient client = new WebClient();
            client.setThrowExceptionOnScriptError(false);
            client.setCssEnabled(false);
            client.getCookieManager().addCookie(cookie);
            HtmlPage page = client.getPage("http://web.batavia-air.com/MyPage/index.php");

            HtmlForm form2 = page.getFormByName("searchbook");

            form2.getSelectByName("ruteBerangkat").setSelectedAttribute(blf.getRuteBerangkat(), true);
            form2.getSelectByName("ruteTujuan").setSelectedAttribute(blf.getRuteTujun(), true);
            form2.getSelectByName("tglBerangkatPergi").setSelectedAttribute(blf.getStrTanggal(), true);
            form2.getSelectByName("blnBerangkatPergi").setSelectedAttribute(blf.getStrBulan(), true);
            form2.getSelectByName("thnBerangkatPergi").setSelectedAttribute(blf.getStrTahun(), true);

            form2.getSelectByName("jmlPenumpang").setSelectedAttribute(blf.getJmlPenumpang(), true);
            form2.getSelectByName("jmlInfant").setSelectedAttribute(blf.getJmlInfant(), true);
            final HtmlSubmitInput bTN = form2.getInputByName("Submit");
            final HtmlPage page3 = bTN.click();

            HtmlForm form3 = page3.getFormByName("pilihPenerbangan");
            form3.getRadioButtonsByName("flightBerangkatPergi").get(radioKe).setChecked(true);
            HtmlSubmitInput btn3 = form3.getInputByName("Submit");

            HtmlPage page4 = btn3.click();


            HtmlForm form4=page4.getFormByName("cekHarga");
            form4.getInputByName("setoejoe").click();
            HtmlSubmitInput btn4=form4.getInputByName("Submit");

            HtmlPage page5=btn4.click();
          
           





            List<HtmlElement> perjalanan = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_PERJALANAN);
            List<HtmlElement> tanggal = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_TANGGAL);
            List<HtmlElement> hari = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_HARI);
            List<HtmlElement> via = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_VIA);
            List<HtmlElement> rute = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_RUTE);
            List<HtmlElement> noPenerbangan = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_NOPENERBANGAN);
            List<HtmlElement> keterangan = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_KETERANGAN);
            List<HtmlElement> paxType = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_PAXTYPE);
            List<HtmlElement> dPerjalanan = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_DPERJALANAN);
            List<HtmlElement> dRute = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_DRUTE);
            List<HtmlElement> klass = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_CLASS);
            List<HtmlElement> hargaDasar = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_HARGADASAR);
            List<HtmlElement> fuelCharge = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_FUELCHARGE);
            List<HtmlElement> pajak = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_PAJAK);
            List<HtmlElement> iwjr = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_IWJR);
            List<HtmlElement> pax = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_PAX);
            List<HtmlElement> kosong = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_);
            List<HtmlElement> detailHargaDewasa = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_TOTALDEWASA);
            List<HtmlElement> totalHarga = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_TOTALHARGA);
            List<HtmlElement> paxPaid = (List<HtmlElement>) page4.getByXPath(FareConstant.XPATH_PAXPAID);

            List<AmbilFlight> result = new ArrayList<AmbilFlight>();

            for (int i = 1; i < tanggal.size(); i++) {
                AmbilFlight amf = parseAmbilFlight(perjalanan, tanggal, hari, via, rute, noPenerbangan, keterangan, paxType, dPerjalanan, dRute, klass, hargaDasar, fuelCharge, pajak, iwjr, pax, kosong, totalHarga, detailHargaDewasa, paxPaid, i);
                amf.setBlf(blf);
                result.add(amf);

            }
            return result;
        } catch (IOException ex) {
            Logger.getLogger(ImplementAmbilFlight.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    public AmbilFlight parseAmbilFlight(List<HtmlElement> perjalanan, List<HtmlElement> tanggal,
            List<HtmlElement> hari,
            List<HtmlElement> via, List<HtmlElement> rute,
            List<HtmlElement> noPenerbangan, List<HtmlElement> keterangan, List<HtmlElement> paxType,
            List<HtmlElement> dPerjalanan,
            List<HtmlElement> dRute, List<HtmlElement> klass, List<HtmlElement> hargaDasar,
            List<HtmlElement> fuelCharge,
            List<HtmlElement> pajak, List<HtmlElement> iwjr, List<HtmlElement> pax, List<HtmlElement> kosong,
            List<HtmlElement> totalHarga, List<HtmlElement> detailHargaDewasa, List<HtmlElement> paxPaid, int i) {
        AmbilFlight af = new AmbilFlight();
        af.setPerjalanan(perjalanan.get(i + 1).asText());
        af.setTanggal(new Date(tanggal.get(i).asText()));
        af.setHari(hari.get(i).asText());
        af.setVia(via.get(i).asText());
        af.setRute(rute.get(i).asText());
        af.setNoPenerbangan(noPenerbangan.get(i).asText());
        af.setKeterangan(keterangan.get(i).asText());
        af.setDetailPaxType(paxType.get(i).asText());
        af.setDetailPerjalanan(dPerjalanan.get(i).asText());
        af.setDetailRute(dRute.get(i).asText());
        af.setDetailKelas(klass.get(i).asText());
        af.setDetailHarga(new BigDecimal(parseHarga(hargaDasar.get(i).asText())));
        af.setDetailFuel(new BigDecimal(parseHarga(fuelCharge.get(i).asText())));
        af.setDetailPajak(new BigDecimal(parseHarga(pajak.get(i).asText())));
        af.setDetailIWJR(new BigDecimal(parseHarga(iwjr.get(i).asText())));
        af.setDetailJumlahPax(new Integer(parseHarga(pax.get(i).asText())));
        af.setDetailLain(new BigDecimal(parseHarga(kosong.get(i).asText())));
        af.setDetailTotalHarga(new BigDecimal(parseHarga(totalHarga.get(i).asText())));
        af.setDetailhargaDewasa(new BigDecimal(parseHarga(detailHargaDewasa.get(i + 1).asText())));
        af.setDetailPAXPaid(new BigDecimal(parseHarga(paxPaid.get(i + 2).asText())));
        return af;
    }

    private String parseHarga(String rawHarga) {
        String harga = rawHarga.trim();
        harga = StringUtils.remove(harga, ",");
        harga = StringUtils.remove(harga, ".00");
        harga = StringUtils.remove(harga, "      ");
        harga = StringUtils.remove(harga, "IDR");
        harga = StringUtils.remove(harga, " ");
        harga = harga.trim();
        return harga;
    }
}
